<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  

  <script>

    //原生ajax

    //1、创建ajax对象
    var xhr = new XMLHttpRequest();
    console.log('xhr ==> ', xhr);

    console.log('创建xhr对象 xhr.readyState ==> ', xhr.readyState);

    //2、监听ajax对象状态的变化
    xhr.onreadystatechange = function () {
      // console.log('this ==> ', this);
      console.log('监听 this.readyState ==> ', this.readyState);

      //获取服务器成功响应的数据
      if (this.readyState === 4 && this.status === 200) {
        //ajax状态为 4 且 http状态码为 200
        
        //获取响应的数据
        var data = JSON.parse(this.responseText);
        console.log('data ==> ', data);
      }

    }

    //API: 接口
    //3、建立服务器(接口地址)连接
    //xhr.open(请求方式, 请求接口地址, 是否异步)
    xhr.open('get', 'http://api.tianapi.com/txapi/ncov/index?key=05c9c231654fad19051ce388d3a14002', true);

    //4、发起http请求
    xhr.send();



  </script>
</body>
</html>